package com.ada.base.security;

import com.ada.base.security.entity.SysUser;
import com.ada.base.security.exception.UserUnloginException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

public class CurrentUserContext {

    public static SysUser current() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof SysUser)
            return (SysUser) principal;
        throw new UserUnloginException(principal.toString());
    }

    public static String username() {
        return current().getUsername();
    }

    public static long userId() {
        return current().getId();
    }

    public static int clientId() {
        return current().getClientId();
    }

    public static boolean isHavePermission(String authority) {
        return current().getAuthorities().contains(new SimpleGrantedAuthority(authority));
    }
}
